package com.ohuang.ohrouter;

import android.content.Context;
import android.content.pm.PackageManager;
import android.util.Log;

import com.ohuang.router_compiler.RouterInterface;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class RouterLoader {
    private static final String TAG = "RouterLoader";
    private final RouterHelper routerHelper;
    private boolean isRegister=false;
    private List<String> data=new ArrayList<>();
    private static final String PATH = "com.ohuang.router.RouterMap$";
    private HashMap<String, String> map = new HashMap<>();

    public RouterLoader(RouterHelper routerHelper) {
        this.routerHelper = routerHelper;
    }


    /**
     *用于字节码插桩
     **/
    private void initData(){

    }

    public void init(Context context) {
        initData();
        if (!isRegister) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    long start = System.currentTimeMillis();
                    Log.d(TAG, "OhRouter.init() start:" + start);
                    try {

                        Set<String> strings = ClassUtils.getFileNameByPackageName(context, PATH);
                        for (String s : strings) {
                            String key = s.replaceFirst("com.ohuang.router.RouterMap\\$", "");

                            map.put(key, s);
                            if (!routerHelper.containsKey(key)) {
                                RouterInterface routerInterface = routerInterfaceForName(s);
                                if (routerInterface != null) {
                                    routerHelper.put(key, routerInterface);
                                }
                            }
                        }
                    } catch (PackageManager.NameNotFoundException | IOException | InterruptedException e) {
                        e.printStackTrace();
                    }
                    long end = System.currentTimeMillis();
                    Log.d(TAG, "OhRouter.init()  end: use Time  " + (end - start));
                }
            }).start();
        }else {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    register();
                }
            }).start();

        }
    }

    private void register(){
        long start = System.currentTimeMillis();
        Log.d(TAG, "OhRouter.init() start:" + start);
        for (String key:data) {
            String path=PATH+key;
            if (!routerHelper.containsKey(key)) {
                RouterInterface routerInterface = routerInterfaceForName(path);
                if (routerInterface != null) {
                    routerHelper.put(key, routerInterface);
                }
            }
        }
        long end = System.currentTimeMillis();
        Log.d(TAG, "OhRouter.init()  end: use Time  " + (end - start));
    }

    private RouterInterface routerInterfaceForName(String className) {
        Class<?> clzz = null;
        RouterInterface routeMap = null;
        try {
            clzz = Class.forName(className);
            routeMap = (RouterInterface) clzz.newInstance();
        } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
            e.printStackTrace();
        }
        if (routeMap != null) {
            routeMap.init();
        }
        return routeMap;
    }


}
